﻿#include "threadquery.h"
#define ASMORDERDBSTR ConnectPoolFactory::getInstance()->getConnectPool(ConnectPoolFactory::AsmOrderDataBase)

threadQuery::threadQuery(QObject *parent) : QObject(parent)
{
    threadTimer = new QTimer(this);
    connect(threadTimer,&QTimer::timeout,this,&threadQuery::threadQueryTimer);
    threadTimer->start(600);
}

void threadQuery::threadQueryTimer()
{
    QElapsedTimer timer;timer.start();
    if(!Servercountbool){return;}
    threadServercount++;

    if(threadServercount>3000){threadServercount=0;}
    if(timer.elapsed()>1000){
        qDebug()<<"Query read data cost:"<<timer.elapsed()<<"ms  "<<QDateTime::currentDateTime().toString("hh:mm ss");timer.restart();
    }
}

QMap<QString,shelfBinInfo> threadQuery::UP_SAPExcelInfotaskCode(QMap<int, SAPExcelInfo> slecetSAPExcelInfoList)
{
    QMap<QString,shelfBinInfo>shelfBinInfoMap;
    connectBuilder build(ASMORDERDBSTR);
    QSqlDatabase sqlDatabase =  build.getOpenDatabase();
    QSqlQuery sqlQuery(sqlDatabase);
    QMap<int, SAPExcelInfo>::const_iterator iter= slecetSAPExcelInfoList.begin();
    while (iter !=slecetSAPExcelInfoList.end()) {
        if(iter!=nullptr){
            if(iter.value().taskStatus>=100){
                sqlQuery.prepare("UPDATE [dbo].[sapExcelinfo] SET [finishTimer]=:finishTimer,"
                                 "[taskStatus]=:taskStatus,[taskStatusDesc]=:taskStatusDesc,[taskError]=:taskError,"
                                 "[errortextEdit]=:errortextEdit "
                                 "WHERE ([SAPTaskIndex]=:SAPTaskIndex);");

                sqlQuery.addBindValue(iter.value().finishTimer);
                sqlQuery.addBindValue(iter.value().taskStatus);
                sqlQuery.addBindValue(iter.value().taskStatusDesc);
                sqlQuery.addBindValue(iter.value().taskError);
                sqlQuery.addBindValue(iter.value().errortextEdit);
                sqlQuery.addBindValue(iter.value().SAPTaskIndex);
            }else{
                sqlQuery.prepare("UPDATE [dbo].[sapExcelinfo] SET [agvNum]=:agvNum,[taskId_ess]=:taskId_ess,[taskId_agv]=:taskId_agv,"
                                 "[taskStatus]=:taskStatus,[taskStatusDesc]=:taskStatusDesc,[taskError]=:taskError,"
                                 "[errortextEdit]=:errortextEdit,[unload]=:unload,[unloadPoint]=:unloadPoint "
                                 "WHERE ([SAPTaskIndex]=:SAPTaskIndex);");

                sqlQuery.addBindValue(iter.value().agvNum);
                sqlQuery.addBindValue(iter.value().taskId_ess);
                sqlQuery.addBindValue(iter.value().taskId_agv);
                sqlQuery.addBindValue(iter.value().taskStatus);
                sqlQuery.addBindValue(iter.value().taskStatusDesc);
                sqlQuery.addBindValue(iter.value().taskError);
                sqlQuery.addBindValue(iter.value().errortextEdit);
                sqlQuery.addBindValue(iter.value().unload);
                sqlQuery.addBindValue(iter.value().unloadPoint);
                sqlQuery.addBindValue(iter.value().SAPTaskIndex);
            }

            if(sqlQuery.exec()){
                qDebug()<<"taskId_ess S"<<iter.value().SAPTaskIndex<<iter.value().taskId_ess
                       <<iter.value().containerCode<<iter.value().taskStatus<<iter.value().taskStatusDesc;
            }else {
                qDebug()<<"taskId_ess E"<<iter.value().SAPTaskIndex<<iter.value().taskId_ess
                       <<iter.value().containerCode<<iter.value().taskStatus<<sqlQuery.lastError();
            }
        }else{
            return shelfBinInfoMap;
        }
        iter++;
    }

    return shelfBinInfoMap;
}

bool threadQuery::UP_cancelTaskSAP(QMap<int, SAPExcelInfo> slecetSAPExcelInfoList)
{

    QMap<QString,shelfBinInfo>shelfBinInfoMap;
    connectBuilder build(ASMORDERDBSTR);
    QSqlDatabase sqlDatabase =  build.getOpenDatabase();
    QSqlQuery sqlQuery(sqlDatabase);
    foreach(SAPExcelInfo iter,slecetSAPExcelInfoList.values()){
        sqlQuery.prepare("UPDATE [dbo].[sapExcelinfo] SET [taskStatus]=:taskStatus,"
                         "[taskStatusDesc]=:taskStatusDesc,[taskError]=:taskError,"
                         "[errortextEdit]=:errortextEdit,[finishTimer]=:finishTimer "
                         "WHERE [SAPTaskIndex]=:SAPTaskIndex;");
        sqlQuery.addBindValue(iter.taskStatus);
        sqlQuery.addBindValue(iter.taskStatusDesc);
        sqlQuery.addBindValue(iter.taskError);
        sqlQuery.addBindValue(iter.errortextEdit);
        sqlQuery.addBindValue(QDateTime::currentDateTime());
        sqlQuery.addBindValue(iter.SAPTaskIndex);

        if(sqlQuery.exec()){
            qDebug()<<"UP_cancelTaskSAP S"<<iter.SAPTaskIndex<<iter.taskId<<iter.Priority
                   <<iter.taskStatus<<iter.cancelTask;
        }else {
            qDebug()<<"UP_cancelTaskSAP E"<<iter.SAPTaskIndex<<iter.taskId<<iter.Priority
                   <<iter.taskStatus<<sqlQuery.lastError();
        }
    }
    return true;

}


//**********************************库存管理********************************//

bool threadQuery::setshelfBinInfo_Status(QMap<QString,shelfBinInfo>shelfBinInfoMap,QString type)
{
    bool update=true;
    connectBuilder build(ASMORDERDBSTR);
    QSqlDatabase sqlDatabase =  build.getOpenDatabase();
    QSqlQuery sqlQuery(sqlDatabase);
    foreach(shelfBinInfo iter,shelfBinInfoMap.values())
    {
        if(type=="Index"){
            //0 空闲  1入库中  2入库完成  3出库中  0出库完成--空闲
            if(iter.status==0 || iter.status==1){
                int SAPTaskIndex=iter.SAPTaskIndex;
                if(iter.status==0){SAPTaskIndex=0;}
                sqlQuery.prepare("UPDATE [dbo].[shelfInfo] SET [status]=:status,[containerCode]=:containerCode,"
                                 "[SAPTaskIndex]=:SAPTaskIndex,[LabelNo]=:LabelNo "
                                 "WHERE ([SAPTaskIndex]=:SAPTaskIndex);");
                sqlQuery.addBindValue(iter.status);
                sqlQuery.addBindValue(iter.containerCode);
                sqlQuery.addBindValue(SAPTaskIndex);
                sqlQuery.addBindValue(iter.LabelNo);
                sqlQuery.addBindValue(iter.SAPTaskIndex);

            }if(iter.status==2 || iter.status==3){
                int SAPTaskIndex=iter.SAPTaskIndex;
                if(iter.status==2){SAPTaskIndex=0;}
                sqlQuery.prepare("UPDATE [dbo].[shelfInfo] SET [status]=:status,[SAPTaskIndex]=:SAPTaskIndex "
                                 "WHERE ([shelfBindesc]=:shelfBindesc);");
                sqlQuery.addBindValue(iter.status);
                sqlQuery.addBindValue(iter.SAPTaskIndex);
                sqlQuery.addBindValue(iter.shelfBindesc);

            }

        }if(type=="Bindesc"){
            if(iter.status==3){
                sqlQuery.prepare("UPDATE [dbo].[shelfInfo] SET [status]=:status,[SAPTaskIndex]=:SAPTaskIndex "
                                 "WHERE ([shelfBindesc]=:shelfBindesc);");
                sqlQuery.addBindValue(iter.status);
                sqlQuery.addBindValue(iter.SAPTaskIndex);
                sqlQuery.addBindValue(iter.shelfBindesc);

            }else{
                sqlQuery.prepare("UPDATE [dbo].[shelfInfo] SET [status]=:status,[containerCode]=:containerCode,"
                                 "[SAPTaskIndex]=:SAPTaskIndex,[LabelNo]=:LabelNo "
                                 "WHERE ([shelfBindesc]=:shelfBindesc);");
                sqlQuery.addBindValue(iter.status);
                sqlQuery.addBindValue(iter.containerCode);
                sqlQuery.addBindValue(iter.SAPTaskIndex);
                sqlQuery.addBindValue(iter.LabelNo);
                sqlQuery.addBindValue(iter.shelfBindesc);
            }

        }

        if(sqlQuery.exec()){
            qDebug()<<"shelfInfo S"<<iter.podIdDesc<<iter.shelfBindesc<<iter.containerCode<<" status:"<<iter.status;
        }else {
            update= false;
            qDebug()<<"shelfInfo E"<<iter.podIdDesc<<iter.shelfBindesc<<iter.containerCode<<" status:"<<iter.status;
        }
    }

    return update;
}

bool threadQuery::UP_deviceI(QMap<QString,deviceI> currentStationInfoMapI)
{

    connectBuilder build(ASMORDERDBSTR);
    QSqlDatabase sqlDatabase =  build.getOpenDatabase();
    QSqlQuery sqlQuery(sqlDatabase);
    foreach(deviceI iter,currentStationInfoMapI.values()){
        sqlQuery.prepare("UPDATE [dbo].[deviceI] SET [stationStatus]=:stationStatus,"
                         "[scanCode]=:scanCode "
                         "WHERE [deviceDesc]=:deviceDesc;");
        sqlQuery.addBindValue(iter.stationStatus);
        sqlQuery.addBindValue(iter.scanCode);
        sqlQuery.addBindValue(iter.deviceDesc);

        if(sqlQuery.exec()){
//            qDebug()<<"UP_deviceI S deviceDesc:"<<iter.deviceDesc<<" stationStatus"<<iter.stationStatus<<" scanCode"<<iter.scanCode;
        }else {
            qDebug()<<"UP_deviceI E deviceDesc:"<<iter.deviceDesc<<" stationStatus"<<iter.stationStatus<<" scanCode"<<iter.scanCode
                   <<" lastError"<<sqlQuery.lastError();
            return false;
        }
    }
    return true;

}

QMap<QString,deviceI> threadQuery::select_StationMap()
{

    QMap<QString, deviceI> currentStationInfoMapI;

    connectBuilder build(ASMORDERDBSTR);
    QSqlDatabase sqlDatabase =  build.getOpenDatabase();
    QSqlQuery sqlQuery(sqlDatabase);

    sqlQuery.setForwardOnly(true);
    sqlQuery.prepare("select * from deviceI where  enable =:enable;");
    sqlQuery.addBindValue(1);
    if(sqlQuery.exec())
    {
        while(sqlQuery.next())
        {
            deviceI deviceItem;
            /******************* 接驳台类型 **************/
            deviceItem.deviceId=sqlQuery.value("deviceId").toInt();                 //设备Id
            deviceItem.deviceIP=sqlQuery.value("deviceIP").toString();              //设备IP
            deviceItem.deviceDesc=sqlQuery.value("deviceDesc").toString();          //设备名称
            deviceItem.deviceType=sqlQuery.value("deviceType").toString();          //设备类型
            deviceItem.enable=sqlQuery.value("enable").toInt();                     //使能
            deviceItem.floor=sqlQuery.value("floor").toInt();                       //优先级

            /******************* 接驳台类型 **************/
            deviceItem.stationStatus=sqlQuery.value("stationStatus").toInt();       //站点类型
            deviceItem.StationType=sqlQuery.value("StationType").toString();        //站点类型
            deviceItem.stationDesc=sqlQuery.value("stationDesc").toString();        //站点名称
            deviceItem.Currentpoint=sqlQuery.value("Currentpoint").toInt();         //当前所在节点
            deviceItem.currentStation=sqlQuery.value("currentStation").toInt();     //当前所在站点
            deviceItem.waitDesc=sqlQuery.value("waitDesc").toString();              //待机点二维码
            deviceItem.waitPoint=sqlQuery.value("waitPoint").toInt();               //当前所在二维码
            deviceItem.scanCode=sqlQuery.value("scanCode").toString();              //工单条码

            currentStationInfoMapI.insert(deviceItem.deviceDesc,deviceItem);

        }
    }

    return currentStationInfoMapI;
}

QMap<int, StationStatusUpdate> threadQuery::select_StationStatusUpdateMap(QString scanCodeI, QDateTime QDateTimeS, QDateTime QDateTimeE)
{

    QMap<int, StationStatusUpdate> StationStatusUpdateMapI;

    connectBuilder build(ASMORDERDBSTR);
    QSqlDatabase sqlDatabase =  build.getOpenDatabase();
    QSqlQuery sqlQuery(sqlDatabase);

    sqlQuery.setForwardOnly(true);

    if(scanCodeI.isEmpty())
    {
        sqlQuery.prepare("select * from StationStatusUpdate where UpdateTime >=:QDateTimeS and UpdateTime <=:QDateTimeE");
        sqlQuery.addBindValue(QDateTimeS);
        sqlQuery.addBindValue(QDateTimeE);

    }else{
        sqlQuery.prepare("select * from StationStatusUpdate where scanCode =:scanCode;");
        sqlQuery.addBindValue(scanCodeI);
    }

    if(sqlQuery.exec())
    {
        while(sqlQuery.next())
        {
            StationStatusUpdate StationStatusUpdateI;
            /******************* 接驳台类型 **************/
            StationStatusUpdateI.ID=sqlQuery.value("ID").toInt();                                   //序列号
            StationStatusUpdateI.StationName=sqlQuery.value("StationName").toString();              //库位名称
            StationStatusUpdateI.StationNameDesc=sqlQuery.value("StationNameDesc").toString();      //库位名称描述
            StationStatusUpdateI.StationStatus=sqlQuery.value("StationStatus").toInt();             //库位状态 0：无料车 1：有任务 2：有料车 3：已烤完
            StationStatusUpdateI.StationStatusDesc=sqlQuery.value("StationStatusDesc").toString();  //库位状态描述
            StationStatusUpdateI.scanCode=sqlQuery.value("scanCode").toString();                    //工单条码
            StationStatusUpdateI.QRcode=sqlQuery.value("QRcode").toInt();                           //库位二维码值
            StationStatusUpdateI.UpdateTime=sqlQuery.value("UpdateTime").toDateTime();              //库位更新时间

            StationStatusUpdateMapI.insert(StationStatusUpdateI.ID, StationStatusUpdateI);

        }
    }

    return StationStatusUpdateMapI;
}

